using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

public class Shell : Singleton<Shell>
{
    #if UNITY_EDITOR_WIN
    public Process CreateShell(string cmd, string args , string workDir = null)
    {
        string execute_args = "/c " + args.TrimEnd('&') + "&exit";

        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = cmd;
        start.Arguments = execute_args;
        start.CreateNoWindow = false;
        start.ErrorDialog = true;
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardInput = true;
        start.RedirectStandardError = true;
        start.WorkingDirectory = workDir == null ? Application.dataPath.Replace("/Assets","") : workDir;
        UnityEngine.Debug.Log(start.WorkingDirectory);
        Process p = Process.Start(start);
        return p;
    }
    #elif UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
    public Process CreateShell(string cmd, string args , string workDir = null)
    {
        var index = args.IndexOf(" ");
        cmd = args.Substring(0,index);
        var execute_args = args.Substring(index + 1);
        ProcessStartInfo start = new ProcessStartInfo();
        start.FileName = cmd;
        start.Arguments = execute_args;
        start.CreateNoWindow = false;
        start.ErrorDialog = true;
        start.UseShellExecute = false;
        start.RedirectStandardOutput = true;
        start.RedirectStandardInput = true;
        start.RedirectStandardError = true;
        start.WorkingDirectory = workDir == null ? Application.dataPath.Replace("/Assets","") : workDir;
        UnityEngine.Debug.Log(start.WorkingDirectory);
        Process p = Process.Start(start);
        return p;
    }
    #endif
}